CREATE OR REPLACE VIEW servicios.vcomm_afip AS 
 SELECT 
    CASE WHEN comm.afipcbtetipo IS NULL THEN
         (select afipcom.tipocomafip
         from afipeqvcom 
         join afip.afipcom afipcom ON afipeqvcom.codificacion::integer = afipcom.tipocomafip
         where ambcfg.idafipeqv = afipeqvcom.idafipeqv AND comm.tipocom = afipeqvcom.tipocom AND comm.letra = afipeqvcom.letra
         order by afipcom.tipocomafip
         limit 1)
    ELSE comm.afipcbtetipo
    END AS tipocomafip,
    comm.nroptovta,
    comm.nro,
    comm.codambsuc,
    comm.fecha,
    comm.id AS idcomm
   FROM comm
     JOIN ambcfg ON true
  WHERE comm.tipocai::text = ANY (ARRAY['W'::character varying::text, 'E'::character varying::text])
  ORDER BY tipocomafip, comm.nroptovta, comm.nro;

ALTER TABLE servicios.vcomm_afip
  OWNER TO abacouser;
COMMENT ON VIEW servicios.vcomm_afip
  IS 'Recupera los comprobantes generados por facturacion electronica con datos de la tabla afip.afipcom';



--==========================
SELECT aaVersion('418.01');
--==========================
